//** Craig Laurent
#import <defaults/defaults.h>		//** for NXDefaultsVector
#import <string.h>
#import "Preferences.h"

#define DEFAULTSOWNER "ConverseLaurent"

#define NUMDEFAULTS 9
#define DEFAULTALERTAUDIO "AlertAudio"
#define DEFAULTALERTSOUND "AlertSound"
#define DEFAULTALERTICON "AlertIcon"
#define DEFAULTALERTWINDOW "AlertWindow"
#define DEFAULTALERTAPPLICATION "AlertApplication"
#define DEFAULTDISCLOSEID "DiscloseID"
#define DEFAULTDISCLOSENAME "DiscloseName"
#define DEFAULTDISCLOSEMACHINE "DiscloseMachine"
#define DEFAULTADDRESSFILE "AddressFile"

@implementation Preferences

- init
{
	const NXDefaultsVector	defaults = {
		{DEFAULTALERTAUDIO, "YES"}
		,{DEFAULTALERTSOUND, "Avon.snd"}
		,{DEFAULTALERTICON, "YES"}
		,{DEFAULTALERTWINDOW, "YES"}
		,{DEFAULTALERTAPPLICATION, "NO"}
		,{DEFAULTDISCLOSEID, "YES"}
		,{DEFAULTDISCLOSENAME, "YES"}
		,{DEFAULTDISCLOSEMACHINE, "YES"}
		,{DEFAULTDISCLOSEMACHINE, "YES"}
		,{DEFAULTADDRESSFILE, ""}
		,{NULL}
	};

	if (self = [super init]) {
		NXRegisterDefaults(DEFAULTSOWNER, defaults);
		soundFile = nil;
		[self loadPreferences];
		return self;
	}
	return nil;
}

- (void)dealloc
{
	if ([self soundFile])
		[self setSoundFile:nil];
	[super dealloc];
}

//****************************************
//** instance methods
- (BOOL)alertAudio
{	return alertAudio; }
- (void)setAlertAudio:(BOOL)audio
{
	alertAudio = audio;
}

- (BOOL)alertIcon
{	return alertIcon; }
- (void)setAlertIcon:(BOOL)icon
{
	alertIcon = icon;
}

- (BOOL)alertWindow
{	return alertWindow; }
- (void)setAlertWindow:(BOOL)window
{
	alertWindow = window;
}

- (BOOL)alertApplication
{	return alertApplication; }
- (void)setAlertApplication:(BOOL)application
{
	alertApplication = application;
}

- (BOOL)discloseID
{	return discloseID; }
- (void)setDiscloseID:(BOOL)dID
{
	discloseID = dID;
}

- (BOOL)discloseName
{	return discloseName; }
- (void)setDiscloseName:(BOOL)dName
{
	discloseName = dName;
}

- (BOOL)discloseMachine
{	return discloseMachine; }
- (void)setDiscloseMachine:(BOOL)dMachine
{
	discloseMachine = dMachine;
}

- (NSString*)soundFile
{	return soundFile;	}
- (void)setSoundFile:(NSString*)file
{
	[soundFile autorelease];
	soundFile = [file retain];
}

- (NSString*)addressFile
{	return addressFile;	}
- (void)setAddressFile:(NSString*)file
{
	[addressFile autorelease];
	addressFile = [file retain];
}


//****************************************
/* loadPreferences - load preference information */
- (void)loadPreferences
{
	const char	*tempDefault;

	//** load preference information from DEFAULTS.
	//**  database takes precedence over NXRegisterDefaults
	if (0 == strcmp("YES", NXGetDefaultValue(DEFAULTSOWNER, DEFAULTALERTAUDIO)))
		[self setAlertAudio:YES];
	else
		[self setAlertAudio:NO];

	tempDefault = NXGetDefaultValue(DEFAULTSOWNER, DEFAULTALERTSOUND);
	if (0 == strcmp(tempDefault, ""))
		[self setSoundFile:nil];
	else
		[self setSoundFile:[NSString stringWithCString:tempDefault]];

	if (0 == strcmp("YES", NXGetDefaultValue(DEFAULTSOWNER, DEFAULTALERTICON)))
		[self setAlertIcon:YES];
	else
		[self setAlertIcon:NO];

	if (0 == strcmp("YES", NXGetDefaultValue(DEFAULTSOWNER, DEFAULTALERTWINDOW)))
		[self setAlertWindow:YES];
	else
		[self setAlertWindow:NO];

	if (0 == strcmp("YES", NXGetDefaultValue(DEFAULTSOWNER, DEFAULTALERTAPPLICATION)))
		[self setAlertApplication:YES];
	else
		[self setAlertApplication:NO];

	if (0 == strcmp("YES", NXGetDefaultValue(DEFAULTSOWNER, DEFAULTDISCLOSEID)))
		[self setDiscloseID:YES];
	else
		[self setDiscloseID:NO];

	if (0 == strcmp("YES", NXGetDefaultValue(DEFAULTSOWNER, DEFAULTDISCLOSENAME)))
		[self setDiscloseName:YES];
	else
		[self setDiscloseName:NO];

	if (0 == strcmp("YES", NXGetDefaultValue(DEFAULTSOWNER, DEFAULTDISCLOSEMACHINE)))
		[self setDiscloseMachine:YES];
	else
		[self setDiscloseMachine:NO];

	tempDefault = NXGetDefaultValue(DEFAULTSOWNER, DEFAULTADDRESSFILE);
	if (0 == strcmp(tempDefault, ""))
		[self setAddressFile:nil];
	else
		[self setAddressFile:[NSString stringWithCString:tempDefault]];
}


//**********
/* savePreferences - save preference information */
- (void)savePreferences
{
	NXDefaultsVector newDefaults = {
		{DEFAULTALERTAUDIO, "YES"}
		,{DEFAULTALERTSOUND, "Avon.snd"}
		,{DEFAULTALERTICON, "YES"}
		,{DEFAULTALERTWINDOW, "YES"}
		,{DEFAULTALERTAPPLICATION, "NO"}
		,{DEFAULTDISCLOSEID, "YES"}
		,{DEFAULTDISCLOSENAME, "YES"}
		,{DEFAULTDISCLOSEMACHINE, "YES"}
		,{DEFAULTADDRESSFILE, ""}
		,{NULL}
	};
	int	defaultsWritten = 0;

	if ([self alertAudio])
		newDefaults[0].value = "YES";
	else
		newDefaults[0].value = "NO";

	newDefaults[1].value = (char*)[[self soundFile] cString];

	if ([self alertIcon])
		newDefaults[2].value = "YES";
	else
		newDefaults[2].value = "NO";

	if ([self alertWindow])
		newDefaults[3].value = "YES";
	else
		newDefaults[3].value = "NO";

	if ([self alertApplication])
		newDefaults[4].value = "YES";
	else
		newDefaults[4].value = "NO";

	if ([self discloseID])
		newDefaults[5].value = "YES";
	else
		newDefaults[5].value = "NO";

	if ([self discloseName])
		newDefaults[6].value = "YES";
	else
		newDefaults[6].value = "NO";

	if ([self discloseMachine])
		newDefaults[7].value = "YES";
	else
		newDefaults[7].value = "NO";

	newDefaults[8].value = (char*)[[self addressFile] cString];

	//** write preference information to defaults.
	defaultsWritten = NXWriteDefaults(DEFAULTSOWNER, newDefaults);
	if (!(NUMDEFAULTS == defaultsWritten))
		NXRunAlertPanel("Preferences Alert", "Unable to write all preferences %d / %d.", NULL, NULL, NULL, defaultsWritten, NUMDEFAULTS);
}


@end
